import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.naming.NamingException;

import unilearn.domain.*;
import unilearn.facade.*;

/**
 * @author Yin Zhang, 5393558
 * @version 0.1, May 10 2008, initial creation
 */
public class StaffUserStory4
{
	private static PrintWriter fileOutput = null;
	
	private static final String SEPARATELINE = "--------------------------------------------------------";
	private static final String OUTPUTPATH = "staffUserStory4";
	private static final String OUTPUTFILENAME = "staffCreateSubject.txt";
	
	public StaffUserStory4() throws FileNotFoundException
	{
		File filePath = new File(OUTPUTPATH);
		
		if(!filePath.exists())
		{
			filePath.mkdir();
		}
		
		fileOutput = new PrintWriter(new FileOutputStream(OUTPUTPATH + File.separator + OUTPUTFILENAME));
	}
	
	public void createNewSubject(String[] subject_details) throws SQLException, NamingException
	{
		Subject subject = new Subject();
		subject.setSubjectCode(subject_details[0]);
		subject.setSubjectName(subject_details[1]);
		subject.setDescription(subject_details[2]);
		subject.setYear(Integer.parseInt(subject_details[3]));
		subject.setSemester(Integer.parseInt(subject_details[4]));
		subject.setConvenorName(subject_details[5]);
		subject.setConvenorEmail(subject_details[6]);
		
		long subjectID = SubjectFacade.createNewSubject(subject);
		
		fileOutput.println("Subject ID " + subjectID + " added");
		
		fileOutput.println(SEPARATELINE);
		
		printSubject(subjectID);
	}
	
	private void printSubject(long subjectID) throws SQLException, NamingException
	{
		Subject subject = SubjectFacade.getSubjectByID(subjectID);
		fileOutput.println("Subject Code: \t\t\t\t" + subject.getSubjectCode());
		fileOutput.println("Subject Name: \t\t\t\t" + subject.getSubjectName());
		fileOutput.println("Subject Description: \t" + subject.getDescription());
		fileOutput.println("Subject Year: \t\t\t\t" + subject.getYear());
		fileOutput.println("Run in Semester: \t\t\t" + subject.getSemester());
		fileOutput.println("Convenor Name: \t\t\t\t" + subject.getConvenorName());
		fileOutput.println("Convenor Email: \t\t\t" + subject.getConvenorEmail());
	}
	
	public void close()
	{
		try
		{
			fileOutput.close();
		}
		finally
		{
			fileOutput = null;
		}
	}
}